package com.youlu.campus.service.activity.impl;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.ActivityTaskInfo;
import com.youlu.campus.entity.ActivityTaskRule;
import com.youlu.campus.entity.QQConfigInfo;
import com.youlu.campus.entity.UserActivityFootPrint;
import com.youlu.campus.entity.UserActivityPointDetail;
import com.youlu.campus.entity.UserActivityTaskInfo;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.UserParticipateActivityTaskInfo;
import com.youlu.campus.entity.VO.req.UserActivityFootPrintQueryVO;
import com.youlu.campus.entity.WechatConfigInfo;
import com.youlu.campus.entity.enums.TaskBizType;
import com.youlu.campus.service.activity.ActivityFootPrintService;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.activity.ActivityTaskRuleService;
import com.youlu.campus.service.activity.ActivityTaskService;
import com.youlu.campus.service.activity.UserActivityTaskInfoService;
import com.youlu.campus.service.ranking.UserActivityPointDetailService;
import com.youlu.campus.service.ranking.UserActivityPointRankingProvinceService;
import com.youlu.campus.service.visitor.VisitorUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ActivityFootPrintServiceImpl implements ActivityFootPrintService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserActivityTaskInfoService userActivityTaskInfoService;

    @Autowired
    private ActivityTaskService activityTaskService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private ActivityTaskRuleService activityTaskRuleService;

    @Autowired
    private UserActivityPointDetailService userActivityPointDetailService;

    @Autowired
    private VisitorUserService visitorUserService;

    @Autowired
    private UserActivityPointRankingProvinceService userActivityPointRankingProvinceService;


    @Override
    public List<UserActivityFootPrint> getUserActivityFootPrint(UserActivityFootPrintQueryVO req) {
        log.info(":>>> 开始获取活动足迹列表:{}", JSON.toJSONString(req));
        if (Objects.isNull(req) || StringUtils.isBlank(req.getActivityId()) || StringUtils.isBlank(req.getUserId())) {
            log.error(":>>> 活动足迹列表参数错误");
            throw new BusinessException("活动足迹列表参数错误");
        }
        Query query =
                new Query().addCriteria(Criteria.where("userId").is(req.getUserId()).and("activityId").is(req.getActivityId()));
        if (StringUtils.isBlank(req.getTaskBizType())) {
            query.addCriteria(Criteria.where("taskBizType").ne(TaskBizType.SHOW_STYLE.name()));
        } else {
            query.addCriteria(Criteria.where("taskBizType").is(req.getTaskBizType()));
        }
        if (StringUtils.isNotBlank(req.getUserTaskId())) {
            query.addCriteria(Criteria.where("userTaskId").is(req.getUserTaskId()));
        }
        query.limit(100);
        List<UserActivityFootPrint> userActivityFootPrints = mongoTemplate.find(query, UserActivityFootPrint.class);
        if (Objects.isNull(userActivityFootPrints)) {
            userActivityFootPrints = new ArrayList<>();
        }
        return userActivityFootPrints;
    }

    @Override
    public UserActivityFootPrint getUserActivityFootPrintByTaskId(UserActivityFootPrintQueryVO req) {
        log.info(":>>> 开始获取活动足迹列表:{}", JSON.toJSONString(req));
        if (Objects.isNull(req) || StringUtils.isBlank(req.getUserTaskId())) {
            log.error(":>>> 活动足迹列表参数错误");
            throw new BusinessException("活动足迹列表参数错误");
        }
        Query query =
                new Query().addCriteria(Criteria.where("userTaskId").is(req.getUserTaskId()));
        return mongoTemplate.findOne(query, UserActivityFootPrint.class);
    }

    @Override
    public boolean createUserActivityFootPrint(UserActivityFootPrint req) {

        log.info(":>>> 开始创建活动足迹:{}", JSON.toJSONString(req));
        if (Objects.isNull(req) || StringUtils.isBlank(req.getActivityId())
                || StringUtils.isBlank(req.getUserId()) || StringUtils.isBlank(req.getImageUrl()) ||
                StringUtils.isBlank(req.getTaskBizType())) {
            log.error(":>>> 创建活动足迹参数错误");
            throw new BusinessException("创建活动足迹参数错误");
        }
        ActivityInfo activityInfo = activityService.findOne(req.getActivityId());
        // 添加大学
        // 添加活动名
        req.setId(null);
        // 待审核
        req.setStatus(0);
        req.setCreatedTime(new Date());
        req.setMobile(req.getUserPO().getMobile());
        Query queryApp = new Query().addCriteria(Criteria.where("appId").is(req.getAppId()));
        switch (req.getPlatform()) {
            case "qq": {
                QQConfigInfo qqConfigInfo = mongoTemplate.findOne(queryApp, QQConfigInfo.class);
                if (Objects.nonNull(qqConfigInfo)) {
                    req.setAppName(qqConfigInfo.getName());
                }
            }
            break;
            case "wechat-mini": {
                WechatConfigInfo configInfo = mongoTemplate.findOne(queryApp, WechatConfigInfo.class);
                if (Objects.nonNull(configInfo)) {
                    req.setAppName(configInfo.getName());
                }
            }
            break;
            default:
                break;
        }

        mongoTemplate.insert(req);
        createUserActivityTaskInfo(activityInfo, req.getTaskBizType(),
                req.getUserId());
        // 感悟类不需要改变状态
        if (!TaskBizType.SHOW_STYLE.name().equals(req.getTaskBizType())) {
            updateUserActivityTaskInfo(activityInfo, req.getTaskBizType(),
                    req.getUserId());
        }
        return true;
    }

    @Override
    public boolean createUserActivityTaskFootPrint(UserActivityFootPrint req) {
        log.info(":>>> 开始创建活动足迹:{}", JSON.toJSONString(req));
        if (Objects.isNull(req) || StringUtils.isBlank(req.getUserTaskId()) || StringUtils.isBlank(req.getImageUrl())) {
            log.error(":>>> 创建活动足迹参数错误");
            throw new BusinessException("创建活动足迹参数错误");
        }
        UserActivityTaskInfo userActivityTaskInfo = userActivityTaskInfoService.find(req.getUserTaskId());
        if (Objects.isNull(userActivityTaskInfo) || userActivityTaskInfo.getStatus() == 2 || userActivityTaskInfo.getStatus() == 4) {
            log.error(":>>> 创建活动足迹参数错误");
            throw new BusinessException("创建活动足迹参数错误");
        }
        UserActivityFootPrintQueryVO userActivityFootPrintQueryVO = new UserActivityFootPrintQueryVO();
        userActivityFootPrintQueryVO.setUserTaskId(req.getUserTaskId());
        UserActivityFootPrint userActivityFootPrintByTaskId = this.getUserActivityFootPrintByTaskId(userActivityFootPrintQueryVO);
        if (Objects.isNull(userActivityFootPrintByTaskId)) {
            req.setUserId(userActivityTaskInfo.getUserId());
            req.setActivityId(userActivityTaskInfo.getActivityId());
            req.setTaskId(userActivityTaskInfo.getTaskId());
            req.setUserTaskId(req.getUserTaskId());
            req.setTaskBizType(userActivityTaskInfo.getTaskBizType().name());
            req.setId(null);
            // 待审核
            req.setStatus(0);
            req.setCreatedTime(new Date());
            req.setMobile(req.getUserPO().getMobile());
            Query queryApp = new Query().addCriteria(Criteria.where("appId").is(req.getAppId()));
            switch (req.getPlatform()) {
                case "qq": {
                    QQConfigInfo qqConfigInfo = mongoTemplate.findOne(queryApp, QQConfigInfo.class);
                    if (Objects.nonNull(qqConfigInfo)) {
                        req.setAppName(qqConfigInfo.getName());
                    }
                }
                break;
                case "wechat-h5":
                case "wechat-mini": {
                    WechatConfigInfo configInfo = mongoTemplate.findOne(queryApp, WechatConfigInfo.class);
                    if (Objects.nonNull(configInfo)) {
                        req.setAppName(configInfo.getName());
                    }
                }
                break;
                default:
                    break;
            }
            mongoTemplate.insert(req);
        } else {
            if (userActivityFootPrintByTaskId.getStatus() == 2) {
                userActivityFootPrintByTaskId.setStatus(0);
            }
            userActivityFootPrintByTaskId.setImageUrl(req.getImageUrl());
            mongoTemplate.save(userActivityFootPrintByTaskId);
        }
        //任务状态设置为待审核
        this.updateUserActivityTaskInfo(req.getUserTaskId(), 1);
        return true;
    }

    @Override
    public boolean deleteUserActivityFootPrint(UserActivityFootPrint req) {
        log.info(":>>> 开始删除活动足迹:{}", JSON.toJSONString(req));
        if (Objects.isNull(req) || StringUtils.isBlank(req.getId())) {
            log.error(":>>> 始删除活动足迹列表参数错误");
            throw new BusinessException("始删除活动足迹列表参数错误:ID不能为空");
        }
        Query query = new Query().addCriteria(Criteria.where("id").is(req.getId()));
        UserActivityFootPrint db = mongoTemplate.findOne(query, UserActivityFootPrint.class);
        if (Objects.isNull(db)) {
            return false;
        }
        DeleteResult deleteResult = mongoTemplate.remove(query, UserActivityFootPrint.class);
        if (deleteResult.getDeletedCount() > 0) {
            ActivityInfo activityInfo = activityService.findOne(db.getActivityId());
            updateUserActivityTaskInfo(activityInfo, db.getTaskBizType(),
                    db.getUserId());
        }
        return true;
    }

    @Override
    public boolean deleteUserActivityTaskFootPrint(UserActivityFootPrint req) {
        log.info(":>>> 开始删除活动足迹:{}", JSON.toJSONString(req));
        if (Objects.isNull(req) || StringUtils.isBlank(req.getId())) {
            log.error(":>>> 始删除活动足迹列表参数错误");
            throw new BusinessException("始删除活动足迹列表参数错误:ID不能为空");
        }
        Query query = new Query().addCriteria(Criteria.where("id").is(req.getId()));
        UserActivityFootPrint db = mongoTemplate.findOne(query, UserActivityFootPrint.class);
        if (Objects.isNull(db)) {
            return false;
        }
        DeleteResult deleteResult = mongoTemplate.remove(query, UserActivityFootPrint.class);
//        if (deleteResult.getDeletedCount() > 0) {
//            ActivityInfo activityInfo = activityService.findOne(db.getActivityId());
//            updateUserActivityTaskInfo(activityInfo, db.getTaskBizType(),
//                    db.getUserId());
//        }
        return true;
    }

    @Override
    public boolean hasUserActivityFootPrint(String userId, String activityId) {
        log.info(":>>> 开始查询活动足迹:{},{}", userId, activityId);

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(activityId)) {
            log.error(":>>> 查询活动足迹参数错误");
            throw new BusinessException("查询活动足迹参数错误:ID不能为空");
        }
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("activityId").is(activityId));
        boolean exist = mongoTemplate.exists(query, UserActivityFootPrint.class);
        log.info(":>>> 用户是否有足迹:{}", userId, activityId);
        return exist;
    }

    @Override
    public HashMap<Integer, Integer> getCount(@NotNull String userId, @NotNull String activityId, @NotNull String taskBizType) {
        log.info(":>>> 开始获取用户:{} ,活动:{}, 任务:{} 各个状态完成数量", userId, activityId, taskBizType);
        HashMap<Integer, Integer> res = new HashMap<>();
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("taskBizType").is(taskBizType).and("activityId").is(activityId));
        List<UserActivityFootPrint> datas = mongoTemplate.find(query, UserActivityFootPrint.class);
        // 状态,0:待审核,1:审核通过,2:驳回,3:无提交
        if (CollectionUtils.isEmpty(datas)) {
            res.put(1, 0);
            res.put(2, 0);
            res.put(0, 0);
            res.put(3, 0);
            log.info(":<<< 结束获取用户:{} ,活动:{}, 任务:{} 各个状态完成数量:{} ", userId, activityId, taskBizType, JSON.toJSONString(res));
            return res;
        }
        for (UserActivityFootPrint u : datas) {
            if (res.containsKey(u.getStatus())) {
                res.put(u.getStatus(), res.get(u.getStatus()) + 1);
            } else {
                res.put(u.getStatus(), 1);
            }
        }
        log.info(":<<< 结束获取用户:{} ,活动:{}, 任务:{} 各个状态完成数量:{} ", userId, activityId, taskBizType, JSON.toJSONString(res));
        return res;
    }

    @Override
    public boolean updateUserActivityFootPrint(UserActivityFootPrint req) {
        log.info(":>>> 开始更新足迹:{}", JSON.toJSONString(req));
        if (Objects.isNull(req) || StringUtils.isBlank(req.getId()) || Objects.isNull(req.getStatus())) {
            log.error(":>>> 更新用户足迹参数错误");
            throw new BusinessException("更新用户足迹参数错误");
        }
        Query query = new Query().addCriteria(Criteria.where("id").is(req.getId()));
        UserActivityFootPrint db = mongoTemplate.findOne(query, UserActivityFootPrint.class);
        if (Objects.isNull(db)) {
            log.error(":>>> 更新用户足迹参数错误:{} 不存在", req.getId());
            throw new BusinessException("更新用户足迹参数错误:数据不存在");
        }
        if (db.getStatus().equals(req.getStatus())) {
            log.error(":>>> 用户足迹:{} 已经是:{}", req.getId(), req.getStatus());
            return true;
        }
        Update update = new Update();
        update.set("status", req.getStatus());
        update.set("updatedTime", new Date());
        if (StringUtils.isNotBlank(req.getComment())) {
            update.set("comment", req.getComment());
        }
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, UserActivityFootPrint.class);
        boolean re = updateResult.getModifiedCount() > 0 ? true : false;
        if (!re) {
            log.error(":>>> 更新用户足迹:{} 状态:{} 直接返回", req.getId(), re);
            return re;
        }
        ActivityInfo activityInfo = activityService.findOne(db.getActivityId());
        ActivityTaskInfo taskInfo1;
        if (StringUtils.isNotBlank(db.getTaskId())) {
            //新增每个活动同类型可以增加多个任务，增加任务id查询
            taskInfo1 = activityTaskService.findById(db.getActivityId(), db.getTaskId());
        } else {
            //老逻辑
            taskInfo1 = activityTaskService.find(db.getActivityId(), TaskBizType.valueOf(db.getTaskBizType()));
        }
        HashMap<Integer, Integer> countMap = this.getCount(db.getUserId(), db.getActivityId(), taskInfo1.getTaskBizType().name());
        Integer num = countMap.get(1);
        if (Objects.isNull(num)) {
            num = req.getStatus();
        }
        /**
         * 线下组织活动状态,0: 未完成,1:已完成,2:未报名,3:待审核,4:审核通过,5:审核不通过
         */
        ActivityTaskRule taskRule = activityTaskRuleService.find(db.getActivityId(), db.getTaskBizType());
        if (Objects.isNull(taskRule)) {
            log.error(":>>> 活动:{} 没有没有配置:{} 任务明细", db.getActivityId(), db.getTaskBizType());
            throw new BusinessException("活动没有配置线下任务规则明细");
        }
        log.info(":>>> 用户:{},活动:{} 审核通过数量为:{},任务完成门限:{},活动的规则:{}", db.getUserId(), db.getActivityId(), num, taskRule.getThreshold(), JSON.toJSONString(taskRule));

        if (StringUtils.isNotBlank(db.getUserTaskId())) {
            UserActivityTaskInfo userActivityTaskInfo = userActivityTaskInfoService.find(db.getUserTaskId());
            if (Objects.isNull(userActivityTaskInfo)) {
                log.error(":>>> 用户活动任务:{} 不存在", db.getUserTaskId());
                return re;
            }
            if (req.getStatus() == 1 && userActivityTaskInfo.getStatus() == 1) {
                this.updateUserActivityTaskInfo(db.getUserTaskId(), 2);
            }
            if (req.getStatus() == 2 && userActivityTaskInfo.getStatus() == 1) {
                this.updateUserActivityTaskInfo(db.getUserTaskId(), 3);
            }
            return re;
        }

        if ("qq-mini-plus-noncontest".equals(activityInfo.getType()) || "qq-mini-plus".equals(activityInfo.getType())
                || "qq-mini-zhongqing-contest".equals(activityInfo.getType()) || "h5++".equals(activityInfo.getType())) {
            log.info(":>>> 活动类型:qq-mini-plus-noncontest 或 qq-mini-plus、qq-mini-zhongqing-contest、h5++");
            TaskBizType taskBizType = TaskBizType.valueOf(db.getTaskBizType());
            UserActivityTaskInfo userActivityTaskInfo = userActivityTaskInfoService.find(db.getUserId(), activityInfo.getId(), taskBizType);
            if (Objects.isNull(userActivityTaskInfo)) {
                log.error(":>>> 用户:{} 活动:{} 任务:{} 不存在", db.getUserId(), activityInfo.getId(), db.getTaskBizType());
                return re;
            }
            log.info(":>>>  足迹ID :{} 审核状态:{},通过 任务门限:{}", req.getId(), req.getStatus(), taskRule.getThreshold());
            if (req.getStatus().intValue() == 1 && num.intValue() >= taskRule.getThreshold()) {
                log.info(":>>>  审核状态:{},通过大于任务门限:{}", req.getId(), req.getStatus(), taskRule.getThreshold());
//                     * 状态,0:待上传,1:待审核,2:审核通过,3:审核不通过
                if (userActivityTaskInfo.getStatus().intValue() == 2) {
                    log.info(":>>>  用户:{} 活动足迹:{},{} 已经是审核通过状态", db.getUserId(), activityInfo.getId(), db.getId());
                    return re;
                }
                UserActivityPointDetail detail = new UserActivityPointDetail();
                detail.setActivityId(db.getActivityId());
                detail.setUserId(db.getUserId());
                UserPO userPO = visitorUserService.getUser(db.getUserId());
                detail.setUserPO(userPO);
                detail.setPoints(taskRule.getPoints().intValue());
                detail.setAction("add");
                switch (db.getTaskBizType()) {
                    case "PRAXIS":
                        detail.setBizDesc("实践行动");
                        detail.setBizType("praxis");
                        break;
                    case "WORK_COLLECTION":
                        detail.setBizDesc("作品征集");
                        detail.setBizType("work-collection");
                        break;
                    default:
                        break;
                }
                userActivityPointDetailService.create(detail);
                userActivityPointRankingProvinceService.updatePoint(userPO, db.getActivityId(), taskRule.getPoints().intValue());
//                userActivityPointRankingService.updatePoint(userPO, db.getActivityId(), taskRule.getPoints().intValue());
                this.updateUserActivityTaskInfo(activityInfo, db.getTaskBizType(),
                        db.getUserId());
                // 审核不通过
            } else if (req.getStatus().intValue() == 2) {
                this.updateUserActivityTaskInfo(activityInfo, db.getTaskBizType(),
                        db.getUserId());
            }
        } else {
            if (req.getStatus().intValue() == 1 && num.intValue() == taskRule.getThreshold()) {
                if (Boolean.TRUE.equals(activityInfo.getAccumulate())) {
                    log.info(":>>> 活动:{} 需要累加", db.getActivityId());
                    UserPO userPO = visitorUserService.getUser(db.getUserId());
                    UserParticipateActivityTaskInfo userParticipateActivityTaskInfo = new UserParticipateActivityTaskInfo();
                    userParticipateActivityTaskInfo.setActivityId(db.getActivityId());
                    ActivityTaskInfo taskInfo = activityTaskService.find(db.getActivityId(), TaskBizType.INVITE);
                    userParticipateActivityTaskInfo.setTaskId(taskInfo.getId());
                    userParticipateActivityTaskInfo.setBizType(TaskBizType.INVITE);
                    userParticipateActivityTaskInfo.setQuantity(taskRule.getPoints());
                    activityService.updateUserTaskAllRanking(userPO, userParticipateActivityTaskInfo, 0);
                    //  activityRankService.updateUserTaskAllRanking(userPO, userParticipateActivityTaskInfo, 0);
                }
            }
        }
        return re;

    }

    @Override
    public boolean batchUpdatePrint(UserActivityFootPrint req) {
        log.info(":>>> 批量更新足迹:{}", JSON.toJSONString(req));
        if (Objects.isNull(req) || org.springframework.util.CollectionUtils.isEmpty(req.getIds()) || Objects.isNull(req.getStatus())) {
            log.error(":>>> 更新用户足迹参数错误");
            throw new BusinessException("更新用户足迹参数错误");
        }
        for (String id : req.getIds()) {
            log.info(":>>> 处理:{} 状态为:{}", id, req.getStatus());
            UserActivityFootPrint todo = new UserActivityFootPrint();
            todo.setId(id);
            todo.setStatus(req.getStatus());
            todo.setComment(req.getComment());
            this.updateUserActivityFootPrint(todo);
        }
        return true;
    }

    private void createUserActivityTaskInfo(ActivityInfo activityInfo, String taskBizTypeStr,
                                            String userId) {
        log.info(":>>> 开始创建用户活动任务:{},{},{}", activityInfo.getId(), taskBizTypeStr, userId);
        if ("qq-mini-plus-noncontest".equals(activityInfo.getType()) ||
                "qq-mini-plus".equals(activityInfo.getType())
                || "qq-mini-zhongqing-contest".equals(activityInfo.getType())
                || "h5++".equals(activityInfo.getType()) || "video".equals(activityInfo.getType())) {
            TaskBizType taskBizType = TaskBizType.valueOf(taskBizTypeStr);
            ActivityTaskInfo taskInfo = activityTaskService.find(activityInfo.getId(), taskBizType);
            UserActivityTaskInfo userActivityTaskInfo = userActivityTaskInfoService.find(userId, activityInfo.getId(), taskBizType);
            if (Objects.isNull(userActivityTaskInfo)) {
                userActivityTaskInfo = new UserActivityTaskInfo();
                userActivityTaskInfo.setActivityId(activityInfo.getId());
                userActivityTaskInfo.setUserId(userId);
                userActivityTaskInfo.setTaskId(taskInfo.getId());
                userActivityTaskInfo.setDoType("all");
                if (TaskBizType.SHOW_STYLE.equals(taskBizType)) {
                    userActivityTaskInfo.setStatus(0);

                } else {
                    userActivityTaskInfo.setStatus(1);
                }

                userActivityTaskInfo.setTaskBizType(taskBizType);
                userActivityTaskInfoService.create(userActivityTaskInfo);
            }
        }
    }

    private void updateUserActivityTaskInfo(@NotNull String userTaskId, @NotNull Integer status) {
        log.info(":>>> 开始修改用户活动任务状态userTaskId:{},status:{}", userTaskId, status);
        UserActivityTaskInfo userActivityTaskInfo = userActivityTaskInfoService.find(userTaskId);
        if (Objects.isNull(userActivityTaskInfo)) {
            log.error(":>>> 用户活动任务:{} 不存在", userTaskId);
            return;
        }
        userActivityTaskInfo.setStatus(status);
        userActivityTaskInfoService.update(userActivityTaskInfo);
    }

    private void updateUserActivityTaskInfo(@NotNull ActivityInfo activityInfo, @NotNull String taskBizTypeStr,
                                            @NotNull String userId) {
        log.info(":>>> 开始修改用户活动任务状态activityInfo:{},taskBizTypeStr:{},userId:{}", activityInfo == null ? null : activityInfo.getId(), taskBizTypeStr, userId);
        if (Objects.isNull(activityInfo) || StringUtils.isBlank(taskBizTypeStr) || StringUtils.isBlank(userId)) {
            log.error(":>>> 修改用户活动任务参数错误");
            return;
        }
        TaskBizType taskBizType = TaskBizType.valueOf(taskBizTypeStr);
        UserActivityTaskInfo userActivityTaskInfo = userActivityTaskInfoService.find(userId, activityInfo.getId(), taskBizType);
        if (Objects.isNull(userActivityTaskInfo)) {
            log.error(":>>> 用户:{} 活动:{} 任务:{} 不存在", userId, activityInfo.getId(), taskBizTypeStr);
            return;
        }
        HashMap<Integer, Integer> countMap = this.getCount(userId, activityInfo.getId(), taskBizType.name());
        log.info(":>>> 用户:{} 活动:{} 任务:{} 审核的状态", userId, activityInfo.getId(), taskBizTypeStr, JSON.toJSONString(countMap));
        ActivityTaskRule taskRule = activityTaskRuleService.find(activityInfo.getId(), taskBizTypeStr);
        if (Objects.isNull(taskRule)) {
            log.error(":>>> 活动:{} 没有没有配置:{} 任务明细", activityInfo.getId(), taskBizTypeStr);
            return;
        }
        //  用户活动任务状态,0:待上传,1:待审核,2:审核通过,3:审核不通过

        // 足迹的状态 :::: 0:待审核,1:审核通过,2:驳回,3:无提交
        if (countMap.containsKey(1) && (countMap.get(1) >= taskRule.getThreshold())) {
            userActivityTaskInfo.setStatus(2);
        } else if (countMap.containsKey(0) && (countMap.get(0) > 0)) {
            userActivityTaskInfo.setStatus(1);
        } else if (countMap.containsKey(1) && countMap.containsKey(0) && countMap.get(1) < 1 && countMap.get(0) < 1) {
            userActivityTaskInfo.setStatus(0);
        } else if (countMap.containsKey(2) && countMap.get(2) > 0 && (!countMap.containsKey(1) || (countMap.containsKey(1) && countMap.get(1) < 1))) {
            userActivityTaskInfo.setStatus(3);
        } else {
            userActivityTaskInfo.setStatus(0);
        }
        userActivityTaskInfoService.update(userActivityTaskInfo);
    }
}
